//
//  QDNewsTableViewController.m
//  QDMetroClient
//
//  Created by YueWen on 2019/3/9.
//  Copyright © 2019 YueWen. All rights reserved.
//

#import "QDNewsTableViewController.h"
#import "QDMetroClient-Swift.h"
#import "QDNewsTableHeaderView.h"
#import "QDWebViewController.h"
#import "QDNewsSectionHeaderView.h"
#import "UITableViewCell+QDTextListModelUpdater.h"

@interface QDNewsTableViewController ()<QDNewsTableHeaderViewAction>

/// 顶部视图
@property (nonatomic, strong) QDNewsTableHeaderView *headerView;
/// 顶部的轮播
@property (nonatomic, copy) NSArray <QDCarouselItem *> *imgList;
/// 资讯列表
@property (nonatomic, strong) NSMutableArray <QDTextListModel *> *textLists;

@end

@implementation QDNewsTableViewController


- (void)loadPropertysAtInitialization {
    [super loadPropertysAtInitialization];
    self.imgList = @[];
    self.textLists = [NSMutableArray arrayWithCapacity:10];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [QDUMEventManager eventObjcWithEvent:@"201"];
    
    self.tableView.rowHeight = 101;
    self.headerView = QDNewsTableHeaderView.new;
    self.headerView.actor = self;
    self.headerView.heziDelegate = self;
    self.headerView.ritl_size = CGSizeMake(self.ritl_width, QDNewsTableHeaderViewHeight());
    self.navigationItem.title = @"资讯";
    
    //请求数据
    [self headerRefreshBeginHandler];
}



- (void)headerRefreshBeginHandler {
    
    self.currentPage = 0;
    [self.tableView.mj_footer resetNoMoreData];
    
    //开始请求header
    [self requestHeaderMessage];
    //开始请求列表数据
    [self requestListMessage];
}


- (void)footerRefreshBeginHandler {
    [self requestListMessage];
}


- (void)registerTableViewCellsAndReusableView {
    [self.tableView registerClass:QDLineTextTableViewCell.class forCellReuseIdentifier:@"cell"];
    [self.tableView registerClass:QDNewsSectionHeaderView.class forHeaderFooterViewReuseIdentifier:@"newsHeader"];
}

#pragma mark - QDNewsTableHeaderViewAction

- (void)qdNewsTableHeaderView:(QDNewsTableHeaderView *)headerView bannerAtIndex:(NSInteger)index {
    
    [QDUMEventManager eventObjcWithEvent:@"200"];
    //进行web跳转
    [self pushToShareWebViewControllerWithUrl:self.imgList[index].url shareTitle:self.imgList[index].title shareImageUrl:self.imgList[index].imgUrl shareDesc:nil autoTitle:false title:@"详情" id:nil qdmetro:true];
//    [self pushToWebViewControllerWithUrl:self.imgList[index].url autoTitle:false title:@"详情" id:nil qdmetro:true];
}



#pragma mark - UITableView

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.textLists.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    QDLineTextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    [cell textUpdateByData:self.textLists[indexPath.row] atIndexPath:indexPath delegate:self associateObject:nil];
    cell.bottomLineView.hidden = (indexPath.row == self.textLists.count - 1);
    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    if (self.textLists.count  == 0){ return UIView.new; }
    
    QDNewsSectionHeaderView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"newsHeader"];
    
    view.titleLabel.text = @"地铁资讯";
    
    return view;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return  self.textLists.count == 0 ? CGFLOAT_MIN : 48;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    QDTextListModel *textModel = self.textLists[indexPath.row];
    
    /// 弹出分享网页控制器
    [self pushToShareWebViewControllerWithUrl:textModel.webUrl shareTitle:textModel.title shareImageUrl:textModel.imageUrl shareDesc:textModel.author autoTitle:false title:@"资讯详情" id:textModel.identifier qdmetro:true];
}


#pragma mark - Request
/// 请求头部数据
- (void)requestHeaderMessage {
    
    /// 请求header信息
    [self requestNewHeaderWithComplete:^(NSDictionary<NSString *,id> * _Nonnull data) {
        
        self.imgList = [NSArray yy_modelArrayWithClass:QDCarouselItem.class json:[data valueForKey:@"imgList"]];
        
        [self.headerView updateWithItems:self.imgList complete:^(QDNewsTableHeaderView * _Nonnull view) {}];
        
        self.tableView.tableHeaderView = self.headerView;
    }];
}


/// 请求列表信息
- (void)requestListMessage {
    
    [self requestNewListWithPage:self.currentPage complete:^(NSArray<NSDictionary<NSString *,id> *> * _Nonnull datas) {
        
        if(self.currentPage == 0){
            [self.textLists removeAllObjects];
        }
        
        [self endHeaderRefreshing];
        [self endFooterRefreshing];
        
        NSArray <QDTextListModel *> *texts = [NSArray yy_modelArrayWithClass:QDTextListModel.class json:datas];
        [self.textLists addObjectsFromArray:texts];
        
        self.currentPage += 1;
        [self.tableView reloadData];
        
        //判断
        if (texts.count < 10){
            [self.tableView.mj_footer resetNoMoreData];
        }
    }];
}

@end
